package com.lanxiaoye.shoppingSystem.common;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.UUID;

/**
 * Created by libing on 2017/2/8.
 */
public class FileUtils {

    public static String createFile(MultipartFile temp_file, HttpServletRequest request)throws Exception{
        String originalFilename = temp_file.getOriginalFilename();
        if (temp_file != null && originalFilename != null && originalFilename.length() > 0){
            //存储图片的路径
            String file_path = request.getSession().getServletContext().getRealPath("upload/customer/");
            //新的图片名称
            String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
            //新的图片
            File newFile = new File(file_path+newFileName);
            //将内存数据写入磁盘
            temp_file.transferTo(newFile);
            return newFileName;
        }
        return null;
    }
}
